from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtWidgets import QSplitter, QPushButton, QHBoxLayout, QTreeWidgetItem

from dao.mysql.MySql import MySql
from dao.mysql.MySqlDao import MySqlDao
from model.DbInfo import DbInfo
from ui.gbq.model.TableTitle import TableTitle
from ui.gbq.model.TreeNode import TreeNode
from ui.gbq.ui.BaseWidget import BaseWidge
from ui.gbq.ui.GTable import GTable
from ui.gbq.ui.GTree import GTree


class DbBrowser(BaseWidge):
    dbInfo: DbInfo
    dbEnInfo = []

    def __init__(self, dbInfo: DbInfo, parent=None):
        super(DbBrowser, self).__init__(parent)
        self.dbInfo = dbInfo
        self.initUI()
        self.center()
        self.initDb()

    def initUI(self):
        self.resize(1000, 600)
        baseWd = QSplitter(Qt.Horizontal)
        baseLayout = QHBoxLayout()
        baseLayout.addWidget(baseWd)
        self.setLayout(baseLayout)
        self.tree = GTree()
        self.tree.setHeaderHidden(True)

    # for i in range(5):
        #     node = TreeNode()
        #     node.label.append(str(i))
        #     for j in range(3):
        #         nodec = TreeNode()
        #         nodec.label.append(str(i) + "+" + str(j))
        #         node.children.append(nodec)
        #     self.dbEnInfo.append(node)

        baseWd.addWidget(self.tree)
        self.tree.setMinimumWidth(200)
        self.table = GTable()
        # self.table.verticalHeader().setVisible(False)
        baseWd.addWidget(self.table)
        baseWd.setSizes([200, 800])
        self.tree.clicked.connect(self.clickedDbTree)

    def initDb(self):
        self.db = MySql()
        self.db.connectDb(self.dbInfo.userName, self.dbInfo.pwd, self.dbInfo.host, self.dbInfo.port)
        self.dao = MySqlDao()
        dbNames = self.dao.showdbs(self.db.cursor)
        for i in dbNames:
            node = TreeNode()
            node.label.append(i)
            tables = self.dao.showTable(self.db.cursor, i)
            for j in tables:
                tableNode = TreeNode()
                tableNode.label.append(j)
                node.children.append(tableNode)
            self.dbEnInfo.append(node)
        self.tree.setData(self.dbEnInfo, self.tree)

    def clickedDbTree(self, index: QModelIndex):
        path = self.treePath(index)
        if len(path) == 2:
            # print(self.dbEnInfo)
            data = self.dao.querry(self.db.cursor, self.dbEnInfo[path[0]].label[0],
                                   self.dbEnInfo[path[0]].children[path[1]].label[0])
            print(data)
            if len(data) == 0:
                return
            d0 = data[0]
            ks = d0.keys()
            titles = []
            for k in ks:
                tableTilte = TableTitle()
                tableTilte.label = k
                tableTilte.pro = k
                titles.append(tableTilte)
            self.table.setData(titles, data)

    # 获取点击树的全路径
    def treePath(self, index: QModelIndex):
        parent = index.parent()
        path = [index.row()]
        while parent.row() != -1:
            path.insert(0, parent.row())
            parent = parent.parent()
        return path
